package com.lzdongrui.backend.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lzdongrui.backend.domain.ApplyLeaveAudit;
import com.lzdongrui.backend.domain.Instructor;
import com.lzdongrui.backend.domain.StudentInstructor;
import com.lzdongrui.backend.domain.dto.ApplyLeaveDTO;
import com.lzdongrui.backend.domain.vo.ApplyLeaveVO;
import com.lzdongrui.backend.domain.vo.BarChartVO;
import com.lzdongrui.backend.mapper.ApplyLeaveAuditMapper;
import com.lzdongrui.backend.mapper.InstructorMapper;
import com.lzdongrui.backend.mapper.StudentinfoMapper;
import com.lzdongrui.common.constant.CacheConstants;
import com.lzdongrui.common.core.domain.entity.LeaveSet;
import com.lzdongrui.common.core.domain.entity.SysDept;
import com.lzdongrui.common.core.domain.entity.SysUser;
import com.lzdongrui.common.exception.ServiceException;
import com.lzdongrui.common.utils.DateUtils;
import com.lzdongrui.common.utils.snowid.SnowflakeIdWorker;
import com.lzdongrui.system.domain.Notification;
import com.lzdongrui.system.mapper.LeaveSetMapper;
import com.lzdongrui.system.mapper.SysDeptMapper;
import com.lzdongrui.system.mapper.SysNoticeMapper;
import com.lzdongrui.system.mapper.SysUserMapper;
import com.lzdongrui.system.service.ISysDeptService;
import com.lzdongrui.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lzdongrui.backend.mapper.ApplyLeaveMapper;
import com.lzdongrui.backend.domain.ApplyLeave;
import com.lzdongrui.backend.service.IApplyLeaveService;
import org.springframework.transaction.annotation.Transactional;

import static com.lzdongrui.common.utils.PageUtils.startPage;
import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * 请假申请Service业务层处理
 *
 * @author wyh
 * @date 2023-05-16
 */
@Service
public class ApplyLeaveServiceImpl implements IApplyLeaveService {
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    private ApplyLeaveMapper applyLeaveMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private LeaveSetMapper leaveSetMapper;

    @Autowired
    private InstructorMapper instructorMapper;

    @Autowired
    private ApplyLeaveAuditMapper applyLeaveAuditMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private StudentinfoMapper studentinfoMapper;

    @Autowired
    private ISysNoticeService sysNoticeService;

    @Autowired
    private ISysNoticeService insertNotification;

    /**
     * 查询请假申请
     *
     * @param id 请假申请主键
     * @return 请假申请
     */
    @Override
    public ApplyLeave selectApplyLeaveById(Long id) {
        return applyLeaveMapper.selectApplyLeaveById(id);
    }

    /**
     * 查询请假申请列表
     *
     * @param applyLeaveDTO 请假申请
     * @return 请假申请
     */
    @Override
    public List<ApplyLeaveVO> selectApplyLeaveList(ApplyLeaveDTO applyLeaveDTO) {
        List<ApplyLeaveVO> retList = new ArrayList<>();

        //判别这个人在的部门是不是最顶级 最顶级及就直接去查所有数据
        SysDept depInfo = deptMapper.selectDeptLeaveById(applyLeaveDTO.getUserDepId());

        if (depInfo.getParentId() == 0) {
            startPage();
            retList = applyLeaveMapper.selectApplyLeaveList(applyLeaveDTO);

        } else {
            //若不是最顶级  判断这个人的是不是辅导员 如果是辅导员就通过申请表 学生表 辅导员班级中间表来拿到要查看的申请数据
            Instructor instructor = new Instructor();
            instructor.setInstructor(applyLeaveDTO.getUserid());
            List<Instructor> insList = instructorMapper.selectInstructorListInApplyLeave(instructor);

            //获取对应的部门的审核组信息
            LeaveSet leaveSet = leaveSetMapper.selectDepInfoAndAdGropInfoByDpId(applyLeaveDTO.getUserDepId());

            if (insList.size() != 0) {
                ApplyLeaveDTO insAppLeave = new ApplyLeaveDTO();
                insAppLeave.setUserid(applyLeaveDTO.getUserid());
                insAppLeave.setDuration(applyLeaveDTO.getDuration());
                insAppLeave.setStuName(applyLeaveDTO.getStuName());
                insAppLeave.setAuditNewLevel(leaveSet.getLevel());
                insAppLeave.setStatus(applyLeaveDTO.getStatus());
                insAppLeave.setAuditDepid(applyLeaveDTO.getUserDepId());
                startPage();
                retList = applyLeaveMapper.selectApplyLeaveListInstructor(insAppLeave);
            } else {
                //不是辅导员就直接通过学院加级别加上级部门去获取他能查看的申请数据
                ApplyLeaveDTO sletDto = new ApplyLeaveDTO();
                sletDto.setCollegeId(leaveSet.getCollegeId());
                sletDto.setAuditNewLevel(leaveSet.getLevel());
                sletDto.setDuration(applyLeaveDTO.getDuration());
                sletDto.setStuName(applyLeaveDTO.getStuName());
                sletDto.setStatus(applyLeaveDTO.getStatus());
                sletDto.setParentId(leaveSet.getParentId());
                startPage();
                retList = applyLeaveMapper.selectApplyLeaveListInCollGrop(sletDto);
            }
        }

        return retList;
    }

    /**
     * 新增请假申请
     *
     * @param applyLeave 请假申请
     * @return 结果
     */
    @Override
    public int insertApplyLeave(ApplyLeaveDTO applyLeave) {
        applyLeave.setId(snowflakeIdWorker.nextId());
        applyLeave.setCreateTime(DateUtils.getNowDate());

        int days = applyLeave.getDuration();//传入的请假天数
        if (!isEmpty(applyLeave.getParentLevelId())){//判断是否为延假申请
            int recDays = recursionDays(applyLeave.getParentLevelId());//递归计算请假天数
            applyLeave.setDuration(recDays+days);//设置合计天数
        }

        Long collegeId = studentinfoMapper.selStudentCollegeIdByStuId(applyLeave.getStu());//获取学院id
        applyLeave.setCollegeId(collegeId);
        Long audit = instructorMapper.selectInstructorIdByGradeId(applyLeave.getGradeId());//获取辅导员id
        applyLeave.setAudit(audit);
        LeaveSet auditLevel = sysUserMapper.selectLevelByAuditId(audit);//获取辅导员部门级别
        applyLeave.setAuditNewLevel(auditLevel.getLevel());//设置当前审核级别

        List<LeaveSet> leaveSets = recursionLeaveSet(auditLevel.getDepartmentId(),collegeId);//根据辅导员部门id递归查询审核组信息

//        List<LeaveSet> leaveSets = leaveSetMapper.selectLeaveSetListByCollegeId(collegeId);//获取学院审核组信息
        setApplyLeaveAuditLevel(leaveSets, applyLeave);//设置审核级别
        applyLeave.setVacationStatus(1);//设置请假状态为未开始
        applyLeave.setStatus(1);//设置审核状态为未审核

        Notification notification = new Notification();
        notification.setViewerUserId(audit);
        sysNoticeService.teacherAuditNoticeAdd(notification);//发送通知

        applyLeave.setDuration(days);//还原传入的请假天数
        applyLeave.setAuditDepid(auditLevel.getDepartmentId());//设置辅导员部门为初始审核部门
        return applyLeaveMapper.insertApplyLeave(applyLeave);
    }

    /**
     * 递归查询审核级别
     */
    public List<LeaveSet> recursionLeaveSet(Long DepartmentId,Long collegeId){
        SysDept sysDept = deptMapper.selectDeptById(DepartmentId);//获取部门信息
        LeaveSet leaveSets = leaveSetMapper.selectLeaveSetListByDepartmentIdAndCollegeId(sysDept.getDeptId(),collegeId);//获取学院审核组信息
//        先判断是否为最顶级部门
        if (sysDept.getParentId() == 0){
            List<LeaveSet> leaveSetList = new ArrayList<>();
            return leaveSetList;
        } else {
            List<LeaveSet> leaveSetList = recursionLeaveSet(sysDept.getParentId(),collegeId);
            leaveSetList.add(leaveSets);
            return leaveSetList;//数据为部门级别正序返回
        }
    }

    /**
     * 计算延假天数
     */
    public int recursionDays(Long id){
        ApplyLeave applyLeave = applyLeaveMapper.selectApplyLeaveById(id);//获取父级请假申请
        if (!isEmpty(applyLeave.getParentLevelId())){//如果父级id为空
            int days = recursionDays(applyLeave.getParentLevelId());//递归计算请假天数
            return days + applyLeave.getDuration();//返回请假天数
        } else {
            return applyLeave.getDuration();//返回请假天数
        }
    }

    /**
     * 检查学生请假时间是否冲突
     */
    @Override
    public boolean checkStuLeaveTime(ApplyLeaveDTO applyLeave) {
        List<ApplyLeaveVO> applyLeaveVOS = applyLeaveMapper.selStuOnStaledated(applyLeave);
        return isBetweenDate(applyLeaveVOS, applyLeave);
    }

    @Override
    public ApplyLeave getExtendLeave(ApplyLeaveDTO applyLeave) {
        return applyLeaveMapper.selExtendLeaveByStu(applyLeave);
    }

    @Override
    public List<ApplyLeaveVO> oldInfoList(ApplyLeaveDTO applyLeaveDTO) {
        List<ApplyLeaveVO> retList = new ArrayList<ApplyLeaveVO>();
        List<ApplyLeaveVO> TopInfo = new ArrayList<ApplyLeaveVO>();
        //拿到延假的源头数据
        TopInfo = getLeaveListInfo(applyLeaveDTO, TopInfo);
        ApplyLeaveDTO det = new ApplyLeaveDTO();
        //回头拿所有的数据
        det.setParentLevelId(TopInfo.get(0).getId());
        retList = getLeaveListInfoASC(det,retList);
        TopInfo.addAll(retList);
        TopInfo.remove(TopInfo.size() - 1);
        return TopInfo;
    }

    @Override
    public List<BarChartVO> BarChart(ApplyLeaveDTO applyLeaveDTO) {
        List<BarChartVO> retList = new ArrayList<>();

        //判别这个人在的部门是不是最顶级 最顶级及就直接去查所有数据
        SysDept depInfo = deptMapper.selectDeptLeaveById(applyLeaveDTO.getUserDepId());

        if (depInfo.getParentId() == 0) {
            startPage();
            retList = applyLeaveMapper.selectApplyLeaveBarChart(applyLeaveDTO);

        } else {
            //若不是最顶级  判断这个人的是不是辅导员 如果是辅导员就通过申请表 学生表 辅导员班级中间表来拿到要查看的申请数据
            Instructor instructor = new Instructor();
            instructor.setInstructor(applyLeaveDTO.getUserid());
            List<Instructor> insList = instructorMapper.selectInstructorListInApplyLeave(instructor);

            if (insList.size() != 0) {
                ApplyLeaveDTO SelectDto = new ApplyLeaveDTO();
                SelectDto.setUserid(applyLeaveDTO.getUserid());
                retList = applyLeaveMapper.selectLeaveBarChartInInstructor(SelectDto);
            } else {
                ApplyLeaveDTO SelectDto = new ApplyLeaveDTO();
                SelectDto.setCollegeId(depInfo.getCollegeId());
                retList = applyLeaveMapper.selectLeaveBarChartNtoInstructor(SelectDto);
            }
        }

        return retList;
    }

    /**
     * 找到最初的请假信息
     *
     * @param applyLeaveDTO
     * @param TopInfo
     * @return
     */
    public List<ApplyLeaveVO> getLeaveListInfo(ApplyLeaveDTO applyLeaveDTO, List<ApplyLeaveVO> TopInfo) {
        List<ApplyLeaveVO> retLists = TopInfo;
        List<ApplyLeaveVO> infoList = applyLeaveMapper.selectApplyLeaveList(applyLeaveDTO);
        for (ApplyLeaveVO applyLeaveVO : infoList) {
            if (applyLeaveVO.getParentLevelId() != null) {
                ApplyLeaveDTO setDto = new ApplyLeaveDTO();
                setDto.setId(applyLeaveVO.getParentLevelId());
                getLeaveListInfo(setDto, retLists);
            } else {
                retLists.add(applyLeaveVO);
            }
        }
        return retLists;
    }

    public List<ApplyLeaveVO> getLeaveListInfoASC(ApplyLeaveDTO applyLeaveDTO, List<ApplyLeaveVO> retList) {
        List<ApplyLeaveVO> retLists = retList;
        List<ApplyLeaveVO> infoList = applyLeaveMapper.selectApplyLeaveList(applyLeaveDTO);
        if(infoList.size() != 0){
            for (ApplyLeaveVO applyLeaveVO : infoList) {
                if (applyLeaveVO.getParentLevelId() != null) {
                    retLists.add(applyLeaveVO);
                    ApplyLeaveDTO setDto = new ApplyLeaveDTO();
                    setDto.setParentLevelId(applyLeaveVO.getId());
                    getLeaveListInfoASC(setDto, retLists);
                } else {
                    retLists.add(applyLeaveVO);
                }
            }
        }
        return retLists;
    }
    /**
     * 判断请假时间是否合理
     */
    public static boolean isBetweenDate(List<ApplyLeaveVO> applyLeaveVOS, ApplyLeaveDTO applyLeave) {
        if (isEmpty(applyLeave.getThinkTime())) {
            for (ApplyLeaveVO applyLeaveVO : applyLeaveVOS) {//判断时间是否在某个假期时间段内
                if (applyLeaveVO.getBeginTime().getTime() >= applyLeave.getBeginTime().getTime() && applyLeaveVO.getEndTime().getTime() <= applyLeave.getEndTime().getTime()) {
                    return true;
                }
            }
        } else {
            for (ApplyLeaveVO applyLeaveVO : applyLeaveVOS) {//判断整个假期是否在某个假期时间段内
                if (applyLeaveVO.getBeginTime().getTime() <= applyLeave.getThinkTime().getTime() && applyLeaveVO.getEndTime().getTime() >= applyLeave.getThinkTime().getTime()) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 设置审核级别
     */
    public void setApplyLeaveAuditLevel(List<LeaveSet> leaveSets, ApplyLeaveDTO applyLeave) {
        int duration = applyLeave.getDuration();//获取请假天数
        for (int i = 0; i < leaveSets.size(); i++) {
            if (leaveSets.get(i).getNumber() * 10 - duration >= 0) {//判断请假天数是否在审核组范围内
                applyLeave.setAuditLevel(leaveSets.get(i).getLevel());//设置审核级别
            } else if (leaveSets.get(i).getNumber() * 10 - duration < 0 && i > 0){
                applyLeave.setAuditLevel(leaveSets.get(i-1).getLevel());//设置上一条审核级别
                break;
            }
//            if (i == leaveSets.size() - 1 && applyLeave.getAuditLevel() == null) {//如果是最后一个审核组并且还没有设置审核级别，则设置顶级审核组
//                applyLeave.setAuditLevel(leaveSets.get(0).getLevel());//设置审核级别
//            }
        }
    }

    /**
     * 修改请假申请
     *
     * @param applyLeave 请假申请
     * @return 结果
     */
    @Override
    public int updateApplyLeave(ApplyLeave applyLeave) {
        applyLeave.setUpdateTime(DateUtils.getNowDate());
        return applyLeaveMapper.updateApplyLeave(applyLeave);
    }

    /**
     * 批量删除请假申请
     *
     * @param ids 需要删除的请假申请主键
     * @return 结果
     */
    @Override
    public int deleteApplyLeaveByIds(Long[] ids) {
        return applyLeaveMapper.deleteApplyLeaveByIds(ids);
    }

    /**
     * 删除请假申请信息
     *
     * @param id 请假申请主键
     * @return 结果
     */
    @Override
    public int deleteApplyLeaveById(Long id) {
        applyLeaveAuditMapper.deleteApplyLeaveAuditByLeaveId(id);
        return applyLeaveMapper.deleteApplyLeaveById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int auditLeave(ApplyLeaveDTO applyLeaveDTO) {
        int ret = 0;
        SysUser userinfo = sysUserMapper.selectUserById(applyLeaveDTO.getAudit());

        //首先判断是通过还是不通过 不通过直接改变主数据的审核状态并且对数据进行记录
        if (applyLeaveDTO.getStatus() == 3) {
            ApplyLeave BadApp = new ApplyLeave();
            BadApp.setId(applyLeaveDTO.getId());
            BadApp.setStatus(applyLeaveDTO.getStatus());
            BadApp.setAudit(applyLeaveDTO.getAudit());
            BadApp.setUpdateTime(DateUtils.getNowDate());
            BadApp.setAuditDepid(userinfo.getDeptId());
            ret = applyLeaveMapper.updateApplyLeave(BadApp);

            ApplyLeaveAudit insetApp = new ApplyLeaveAudit();
            insetApp.setId(snowflakeIdWorker.nextId());
            insetApp.setApplyLeaveId(applyLeaveDTO.getId());
            insetApp.setAudit(applyLeaveDTO.getAudit());
            insetApp.setAuditName(userinfo.getNickName());
            insetApp.setReason(applyLeaveDTO.getReason());
            insetApp.setStatus(applyLeaveDTO.getStatus());
            insetApp.setCreateTime(DateUtils.getNowDate());
            applyLeaveAuditMapper.insertApplyLeaveAudit(insetApp);

            //发送通知
            Notification notification = new Notification();
            notification.setId(snowflakeIdWorker.nextId());
            notification.setViewerUserId(applyLeaveDTO.getStu());
            insertNotification.studentLeaveNotpassAdd(notification);
        } else {
            //检测当前操作是否有跟换流程行为
            if (applyLeaveDTO.getProcess() == 1) {
                //查询审核部门是否存在人员
                SysUser sysUserCounts = new SysUser();
                sysUserCounts.setDeptId(applyLeaveDTO.getAuditDp());
                List<SysUser> sysUserCount = sysUserMapper.selectuserListNotConnection(sysUserCounts);

                if (sysUserCount.size() <= 0) {
                    throw new ServiceException("当前所选部门下不存在人员！请联系负责人或更换部门审核！");
                }

                //获取到跟换审核部门的级别信息并替换下一个审核级别
                LeaveSet leaveSet = leaveSetMapper.selectDepInfoAndAdGropInfoByDpId(applyLeaveDTO.getAuditDp());
                //查询该学生的辅导员信息
                StudentInstructor studentInstructor = applyLeaveMapper.selectStudenTeacherbyId(applyLeaveDTO.getStu());

                ApplyLeave newApp = new ApplyLeave();
                newApp.setId(applyLeaveDTO.getId());
                newApp.setAudit(applyLeaveDTO.getAudit());

                //这两个是设置最终级别和审核级别一致 直接为最终审核
                newApp.setAuditNewLevel(leaveSet.getLevel());
                newApp.setAuditLevel(leaveSet.getLevel());

                newApp.setUpdateTime(DateUtils.getNowDate());
                newApp.setAuditDepid(leaveSet.getDepartmentId());
                ret = applyLeaveMapper.updateApplyLeave(newApp);

                ApplyLeaveAudit insetApp = new ApplyLeaveAudit();
                insetApp.setId(snowflakeIdWorker.nextId());
                insetApp.setApplyLeaveId(applyLeaveDTO.getId());
                insetApp.setAudit(applyLeaveDTO.getAudit());
                insetApp.setAuditName(userinfo.getNickName());
                insetApp.setReason(applyLeaveDTO.getReason());
                insetApp.setStatus(applyLeaveDTO.getStatus());
                insetApp.setCreateTime(DateUtils.getNowDate());
                applyLeaveAuditMapper.insertApplyLeaveAudit(insetApp);

                //如果更换的部门为辅导员的部门
                if (studentInstructor.getDeptId().equals(leaveSet.getDepartmentId())) {
                    //发送通知
                    Notification notification = new Notification();
                    notification.setId(snowflakeIdWorker.nextId());
                    notification.setViewerUserId(studentInstructor.getInstructor());
                    insertNotification.teacherAuditNoticeAdd(notification);
                } else {
                    SysUser sysUser = new SysUser();
                    sysUser.setDeptId(leaveSet.getDepartmentId());
                    List<SysUser> sysUsers = sysUserMapper.selectuserListNotConnection(sysUser);

                    for (SysUser sysUserFor : sysUsers) {
                        //发送通知
                        Notification notification = new Notification();
                        notification.setId(snowflakeIdWorker.nextId());
                        notification.setViewerUserId(sysUserFor.getUserId());
                        insertNotification.teacherAuditNoticeAdd(notification);
                    }
                }
            } else {
                //获取当前部门的下一级别信息
                LeaveSet leaveSet = leaveSetMapper.selectDepInfoAndAdGropInfoByDpId(userinfo.getDeptId());
                int leaveGrop = leaveSet.getLevel();

                //若是通过就检测这个人是否是最终审核人 如果为最终审核人就可以对主数据进行更改并且记录
                if (leaveGrop == applyLeaveDTO.getAuditLevel()) {
                    ApplyLeave finallyApp = new ApplyLeave();
                    finallyApp.setId(applyLeaveDTO.getId());
                    finallyApp.setStatus(applyLeaveDTO.getStatus());
                    finallyApp.setAudit(applyLeaveDTO.getAudit());
                    finallyApp.setUpdateTime(DateUtils.getNowDate());
                    finallyApp.setAuditDepid(userinfo.getDeptId());
                    ret = applyLeaveMapper.updateApplyLeave(finallyApp);

                    ApplyLeaveAudit insetApp = new ApplyLeaveAudit();
                    insetApp.setId(snowflakeIdWorker.nextId());
                    insetApp.setApplyLeaveId(applyLeaveDTO.getId());
                    insetApp.setAudit(applyLeaveDTO.getAudit());
                    insetApp.setAuditName(userinfo.getNickName());
                    insetApp.setReason(applyLeaveDTO.getReason());
                    insetApp.setStatus(applyLeaveDTO.getStatus());
                    insetApp.setCreateTime(DateUtils.getNowDate());
                    applyLeaveAuditMapper.insertApplyLeaveAudit(insetApp);

                    //发送通知
                    Notification notification = new Notification();
                    notification.setId(snowflakeIdWorker.nextId());
                    notification.setViewerUserId(applyLeaveDTO.getStu());
                    insertNotification.studentLeavePassAdd(notification);

                } else if (leaveGrop > applyLeaveDTO.getAuditLevel()) {
                    //首先获取下个审核级别的信息
                    LeaveSet leaveSet1 = new LeaveSet();
                    leaveSet1.setCollegeId(leaveSet.getCollegeId());
                    leaveSet1.setLevel(applyLeaveDTO.getAuditNewLevel() - 1);
                    leaveSet1.setParentId(leaveSet.getParentId());
                    LeaveSet LeaveInfo = leaveSetMapper.selectUpLeaveSetListByCollegeIdAndLevel(leaveSet1);

                    //查询审核部门是否存在人员
                    SysUser sysUserCounts = new SysUser();
                    sysUserCounts.setDeptId(LeaveInfo.getDepartmentId());
                    List<SysUser> sysUserCount = sysUserMapper.selectuserListNotConnection(sysUserCounts);

                    if (sysUserCount.size() <= 0) {
                        throw new ServiceException("当前所选部门下不存在人员！请联系负责人或更换部门审核！");
                    }

                    //若不是最终审核 对主数据 当前审核级别-1(从下至上)  且记录数据
                    ApplyLeave notFinallyApp = new ApplyLeave();
                    notFinallyApp.setId(applyLeaveDTO.getId());
                    notFinallyApp.setAudit(applyLeaveDTO.getAudit());
                    notFinallyApp.setAuditNewLevel(applyLeaveDTO.getAuditNewLevel() - 1);
                    notFinallyApp.setUpdateTime(DateUtils.getNowDate());
                    notFinallyApp.setAuditDepid(leaveSet.getParentId());
                    ret = applyLeaveMapper.updateApplyLeave(notFinallyApp);

                    ApplyLeaveAudit insetApp = new ApplyLeaveAudit();
                    insetApp.setId(snowflakeIdWorker.nextId());
                    insetApp.setApplyLeaveId(applyLeaveDTO.getId());
                    insetApp.setAudit(applyLeaveDTO.getAudit());
                    insetApp.setAuditName(userinfo.getNickName());
                    insetApp.setReason(applyLeaveDTO.getReason());
                    insetApp.setStatus(applyLeaveDTO.getStatus());
                    insetApp.setCreateTime(DateUtils.getNowDate());
                    applyLeaveAuditMapper.insertApplyLeaveAudit(insetApp);

                    SysUser sysUser = new SysUser();
                    sysUser.setDeptId(LeaveInfo.getDepartmentId());
                    List<SysUser> sysUsers = sysUserMapper.selectuserListNotConnection(sysUser);

                    for (SysUser sysUserFor : sysUsers) {
                        //发送通知
                        Notification notification = new Notification();
                        notification.setId(snowflakeIdWorker.nextId());
                        notification.setViewerUserId(sysUserFor.getUserId());
                        insertNotification.teacherAuditNoticeAdd(notification);
                    }

                } else {
                    throw new ServiceException("审核级别不存在");
                }
            }
        }
        return ret;
    }

    @Override
    public List<ApplyLeaveVO> selClientStuApplyLeave(ApplyLeaveDTO applyLeaveDTO) {
        return applyLeaveMapper.selClientStuApplyLeave(applyLeaveDTO);
    }

    @Override
    public List<ApplyLeaveVO> selClientStuApplyLeaveLast(Long stuId) {
        return applyLeaveMapper.selClientStuApplyLeaveLast(stuId);
    }


}
